package com.cnstrong.webview2.view;

import android.graphics.Canvas;
import android.graphics.Paint;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.SoundEffectConstants;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;

public class MagicGesturePolicy {

    private static final String TAG = "MagicGesturePolicy";
    private static final int MSG_TIMING = 1;
    private static final int MSG_LEAKING = 2;
    private static final int MAX_GESTURE_COUNT = 5;
    private static final int MSG_TIMING_DELAY = 1000 * 10;

    private final GestureHandler mGestureHandler;
    private List<MagicGesture> mSegmentList;
    private final MagicGestureView mMagicGestureView;

    private List<MagicGesture> mLeakSegmentList;

    public MagicGesturePolicy(MagicGestureView magicGestureView) {
        mMagicGestureView = magicGestureView;
        mGestureHandler = new GestureHandler(this);
    }

    public void drawMagicGesture(Canvas canvas, Paint paint) {
        if (mSegmentList != null) {
            for (MagicGesture gesture : mSegmentList) {
                if (gesture.isRightTop()) {
                    canvas.drawRect(gesture.getRightTopRect(), paint);
                } else if (gesture.isLeftTop()) {
                    canvas.drawRect(gesture.getLeftTopRect(), paint);
                } else if (gesture.isLeftBottom()) {
                    canvas.drawRect(gesture.getLeftBottomRect(), paint);
                } else if (gesture.isRightBottom()) {
                    canvas.drawRect(gesture.getRightBottomRect(), paint);
                }
            }
        }
        if (mLeakSegmentList != null) {
            for (MagicGesture gesture : mLeakSegmentList) {
                if (gesture.isRightTop()) {
                    canvas.drawRect(gesture.getRightTopRect(), paint);
                } else if (gesture.isLeftTop()) {
                    canvas.drawRect(gesture.getLeftTopRect(), paint);
                } else if (gesture.isLeftBottom()) {
                    canvas.drawRect(gesture.getLeftBottomRect(), paint);
                } else if (gesture.isRightBottom()) {
                    canvas.drawRect(gesture.getRightBottomRect(), paint);
                }
            }
        }
    }

    public boolean addLeakGesture(MagicGesture gesture) {
        if (mLeakSegmentList == null) {
            mLeakSegmentList = new ArrayList<>();
        }

        if (!mGestureHandler.hasMessages(MSG_LEAKING)) {
            mGestureHandler.sendEmptyMessageDelayed(MSG_LEAKING, MSG_TIMING_DELAY);
        }

        if (mLeakSegmentList.size() == MAX_GESTURE_COUNT) {
            return false;
        }

        if (gesture.isRightBottom()) {
            mLeakSegmentList.add(gesture);
            mMagicGestureView.playSoundEffect(SoundEffectConstants.CLICK);
            // 如果手势够数目，不再收集，去做其他业务
            if (mLeakSegmentList.size() == MAX_GESTURE_COUNT) {
                mGestureHandler.removeMessages(MSG_LEAKING);
                mGestureHandler.sendEmptyMessage(MSG_LEAKING);
            }
            return true;
        }

        return false;
    }


    public boolean addGesture(MagicGesture gesture) {
        if (mSegmentList == null) {
            mSegmentList = new ArrayList<>();
        }

        // 设置定时器
        // 开始在10s收集所有手势，
        if (!mGestureHandler.hasMessages(MSG_TIMING)) {
            mGestureHandler.sendEmptyMessageDelayed(MSG_TIMING, MSG_TIMING_DELAY);
        }

        // 手势收集满，不在收集，等待手势清空
        if (mSegmentList.size() == MAX_GESTURE_COUNT) {
            return false;
        }

        // 第1个点时右上边
        if (mSegmentList.size() == 0) {
            // gesture是右上边，添加进去
            if (gesture.isRightTop()) {
                mSegmentList.add(gesture);
                mMagicGestureView.playSoundEffect(SoundEffectConstants.CLICK);
                return true;
            }
        }

        // 第二个点是左上边
        if (mSegmentList.size() == 1) {
            // gesture是左上边，添加进去
            if (gesture.isLeftTop()) {
                mSegmentList.add(gesture);
                mMagicGestureView.playSoundEffect(SoundEffectConstants.CLICK);
                return true;
            }
        }

        // 第三个点左下边
        if (mSegmentList.size() == 2) {
            // gesture是左下边，添加进去
            if (gesture.isLeftBottom()) {
                mSegmentList.add(gesture);
                mMagicGestureView.playSoundEffect(SoundEffectConstants.CLICK);
                return true;
            }
        }

        // 第四个点时右下边
        if (mSegmentList.size() == 3) {
            // gesture是右下边，添加进去
            if (gesture.isRightBottom()) {
                mSegmentList.add(gesture);
                mMagicGestureView.playSoundEffect(SoundEffectConstants.CLICK);
                return true;
            }
        }

        // 第五个需要画一条线
        if (mSegmentList.size() == 4) {
            // gesture是右上边，添加进去
            if (gesture.isLineGesture()) {
                mSegmentList.add(gesture);
                mMagicGestureView.playSoundEffect(SoundEffectConstants.CLICK);
                // 如果手势正确，快速关闭界面
                mGestureHandler.removeMessages(MSG_TIMING);
                mGestureHandler.sendEmptyMessage(MSG_TIMING);
                return true;
            }
        }
        return false;
    }

    private static class GestureHandler extends Handler {
        private final WeakReference<MagicGesturePolicy> mGesturePolicy;

        private GestureHandler(MagicGesturePolicy collector) {
            super(Looper.getMainLooper());
            mGesturePolicy = new WeakReference(collector);
        }

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            MagicGesturePolicy gesturePolicy = mGesturePolicy.get();
            if (gesturePolicy == null) {
                return;
            }
            switch (msg.what) {
                case MSG_TIMING: {
                    // 时间到了，
                    if (gesturePolicy.mSegmentList.size() == MAX_GESTURE_COUNT) {
                        MagicGesture gesture = gesturePolicy.mSegmentList.get(gesturePolicy.mSegmentList.size() - 1);
                        if (gesture.getGestureTyp() == MagicGesture.TYPE_LINE) {
                            if (gesturePolicy.mMagicGestureView != null) {
                               MagicGestureView.GestureProcessor processor = gesturePolicy.mMagicGestureView.getGestureProcessor();
                                if (processor != null) {
                                   processor.handleGesture(new MagicGestureEvent(MagicGestureEvent.Event.GESTURE_RIGHT));
                                }
                            }
                        }
                    }
                    gesturePolicy.mSegmentList.clear();
                    if (gesturePolicy.mMagicGestureView != null){
                        gesturePolicy.mMagicGestureView.invalidate();
                    }
                    break;
                }
                case MSG_LEAKING: {
                    // 时间到了
                    if (gesturePolicy.mLeakSegmentList.size() == MAX_GESTURE_COUNT) {
                        // 数目达到后，发消息出去
                        if (gesturePolicy.mMagicGestureView != null) {
                            MagicGestureView.GestureProcessor processor = gesturePolicy.mMagicGestureView.getGestureProcessor();
                            if (processor != null) {
                                processor.handleGesture(new MagicGestureEvent(MagicGestureEvent.Event.GESTURE_LEAK));
                            }
                        }
                    }
                    gesturePolicy.mLeakSegmentList.clear();
                    if (gesturePolicy.mMagicGestureView != null){
                        gesturePolicy.mMagicGestureView.invalidate();
                    }
                    break;
                }
                default:
                    break;
            }
        }
    }

}
